package com.kk;

import com.kk.utils.AppHandler;
import de.felixroske.jfxsupport.AbstractJavaFxApplicationSupport;
import de.felixroske.jfxsupport.SplashScreen;
import javafx.stage.Stage;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

/**
 * the class of javafx-nesting-ui
 *
 * <p>
 * .
 *
 * @author little_lunatic
 * @date 2022-12-28
 */
@SpringBootApplication
public class NestingApplication extends AbstractJavaFxApplicationSupport {

    static {
        AppHandler.initialize();
        AppHandler.setStartup(true);
        // 加载动画
        SplashScreen.setImagePath("/imgs/start.png");
    }

    public static void main(String[] args) {
        launch(NestingApplication.class, null, null, args);
    }

    /**
     * 开始初始化UI前
     *
     * @param stage stage
     * @param ctx   spring上下文
     */
    @Override
    public void beforeInitialView(Stage stage, ConfigurableApplicationContext ctx) {
        super.beforeInitialView(stage, ctx);
        AbstractJavaFxApplicationSupport.WEBVIEW.setContextMenuEnabled(false);

        // 设置关闭按钮触发事件
        stage.setOnCloseRequest(event ->{
            System.out.println("==================close==============");
        });

        // 禁用右键
        AbstractJavaFxApplicationSupport.WEBVIEW.setContextMenuEnabled(false);
        // 设置为全屏
        stage.setFullScreen(false);
        // 设置全屏提示语 默认为 按ESC退出全屏
        stage.setFullScreenExitHint("按 ESC 退出全屏");
        // 禁用退出全屏
//        stage.setFullScreenExitKeyCombination(KeyCombination.NO_MATCH);
    }


    /**
     * 重写url
     *
     * @return url
     */
    @Override
    protected String getUrl() {
        String url = super.getUrl();
        String prefix = "file:";

        String workDir = System.getProperty("user.dir");

        if (url.startsWith(prefix)) {
            url = url.replace(prefix, "");
            return prefix + workDir + "/" + url;
        }
        return url;
    }

    @Override
    public void stop() throws Exception {
        super.stop();
        AppHandler.exit();
    }
}
